<?php
/**
 * Create by 11li
 * Date 2020/3/6
 * Time 10:33
 *
 */

namespace app\common\service;


use app\common\model\PromoterCount;
use think\Db;

class PromoterCountService extends BaseService
{
	
	/**
	 * 添加推广员单个商品的佣金统计
	 * @param $userId
	 * @param $goodsId
	 * @param int $clickNumber
	 * @param int $orderNumber
	 * @param int $clickCommission
	 * @param int $orderCommission
	 * @param int $shareCommission
	 * @return bool
	 * @throws \think\Exception
	 * @throws \think\exception\DbException
	 * @throws \think\exception\PDOException
	 */
	public function addCommission($userId, $goodsId, $clickNumber = 0, $orderNumber = 0, $clickCommission = 0, $orderCommission = 0, $shareCommission = 0)
	{
		$row = PromoterCount::get(['user_id' => $userId]);
		$data = [
			'click_number' => $row ? $row['click_number'] + $clickNumber : $clickNumber,
			'order_number' => $row ? $row['order_number'] + $orderNumber : $orderNumber,
			'click_commission' => $row ? $row['click_commission'] + $clickCommission : $clickCommission,
			'order_commission' => $row ? $row['order_commission'] + $orderCommission : $orderCommission,
			'share_commission' => $row ? $row['share_commission'] + $shareCommission : $shareCommission,
			'user_id' => $userId,
			'goods_id' => $goodsId,
			'update_time' => time()
		];
		
		if($row) {
			$res = Db::name('promoterCount')
				->where(['id' => $row['id']])
				->update($data);
		} else {
			$data['create_time'] = time();
			$res = Db::name('promoterCount')
				->insert($data);
		}
		
		if(!$res) {
			return false;
		}
		return true;
	}
}